package com.cloudspace.yunloan.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.cloudspace.yunloan.YunloanApp;

public class ActivityUtil {
    public static void startApp(Context mContext, String url) {
        Intent intent = new Intent();
        if (url.startsWith("com.")) {
            intent = YunloanApp.getInstance().getPackageManager().getLaunchIntentForPackage(url);
        } else if (url.contains("://")) {
            Uri uri = Uri.parse(url);
            intent.setData(uri);
            intent.setAction(Intent.ACTION_VIEW);
        }
        mContext.startActivity(intent);
    }

    public static boolean isActivityValid(Activity activity) {
        if (activity == null) {
            return false;
        }

        if (activity.isFinishing()) {
            return false;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (activity.isDestroyed()) {
                return false;
            }
        }

        return true;
    }
}
